package com.zh.work.controller.platform.agent;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.work.dao.platform.agent.AgentRoleMapper;
import com.zh.work.dao.platform.agent.AgentUserMapper;
import com.zh.work.entity.agent.AgentRoleEntity;
import com.zh.work.entity.agent.AgentUserEntity;
import com.zh.common.entity.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 代理商管理员控制器
 * @Author: Li.HuiNeng
 * @Create in: 2019/9/2
 * @Version: 1.0.1
 */
@Api(value = "代理商管理员控制器", produces = "application/json", tags = {"代理商管理员控制器"})
@RestController
@RequestMapping("/agentUser")
public class AgentUserController {

    @Autowired
    private AgentUserMapper agentUserMapper;

    @Autowired
    private AgentRoleMapper agentRoleMapper;

    @ApiOperation(value = "添加代理商操作员", tags = {"putAgentUser"}, notes = "添加代理商操作员")
    @PutMapping("/putAgentUser")
    @ResponseBody
    public JSONResult<AgentUserEntity> putAgentUser(@ApiParam(value = "添加代理商操作员", name = "agentUser") @RequestBody AgentUserEntity agentUserEntity) {
        JSONResult<AgentUserEntity> jsonResult = new JSONResult<AgentUserEntity>();
        try {
            if (agentUserEntity.insert()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }




    @ApiOperation(value = "分页查询代理商管理员数据集", tags = {"getPagesAgentUsers"}, notes = "分页查询代理商管理员据集")
    @GetMapping("/getPagesAgentUsers")
    public JSONResult<List<AgentUserEntity>> getPagesAgentUsers(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "代理商管理员实体类", name = "agentUserEntity") AgentUserEntity agentUserEntity) {
        JSONResult<List<AgentUserEntity>> jsonResult = new JSONResult<List<AgentUserEntity>>();
        try {
            IPage<AgentUserEntity> result = this.agentUserMapper.selectPage(
                    new Page<AgentUserEntity>(pageNo, pageSize),
                    new QueryWrapper<AgentUserEntity>()
                            .like(StringUtils.isNotEmpty(agentUserEntity.getBranchNo()), "branchno", agentUserEntity.getBranchNo())
//                            .like(StringUtils.isNotEmpty(agentUserEntity.getTrueName()), "true_name", agentUserEntity.getTrueName())
//                            .like(StringUtils.isNotEmpty(agentUserEntity.getLoginName()), "user_roler", agentUserEntity.getLoginName())
//                            .like(StringUtils.isNotEmpty(agentUserEntity.getTelephone()), "mobile", agentUserEntity.getTelephone())
//                            .eq(Objects.nonNull(agentUserEntity.getStatus()), "status", agentUserEntity.getStatus())
            );
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());

//            redisTemplate.opsForValue().set("test", list);
//            List<ParamEntity> list1 = (List<ParamEntity>)redisTemplate.opsForValue().get("test");
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    // 以下是代理商角色段

    @ApiOperation(value = "添加代理商角色", tags = {"putAgentRole"}, notes = "添加代理商角色")
    @PutMapping("/putAgentRole")
    public JSONResult<AgentRoleEntity> putAgentRole(@ApiParam(value = "添加代理商角色", name = "agentRole") AgentRoleEntity agentRoleEntity) {
        JSONResult<AgentRoleEntity> jsonResult = new JSONResult<AgentRoleEntity>();
        try {
            if (agentRoleEntity.insert()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询代理商角色数据集", tags = {"getPagesAgentRoles"}, notes = "分页查询代理商角色数据集")
    @GetMapping("/getPagesAgentRoles")
    public JSONResult<List<AgentRoleEntity>> getPagesAgentRoles(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "代理商角色实体类", name = "agentRoleEntity") AgentRoleEntity agentRoleEntity) {
        JSONResult<List<AgentRoleEntity>> jsonResult = new JSONResult<List<AgentRoleEntity>>();
        try {
            IPage<AgentRoleEntity> result = this.agentRoleMapper.selectPage(
                    new Page<AgentRoleEntity>(pageNo, pageSize),
                    new QueryWrapper<AgentRoleEntity>()
                            .eq(StringUtils.isNotEmpty(agentRoleEntity.getBranchNo()), "branchno", agentRoleEntity.getBranchNo())
                            .like(StringUtils.isNotEmpty(agentRoleEntity.getAgentNo()), "agentNo", agentRoleEntity.getAgentNo())
//                            .like(StringUtils.isNotEmpty(agentRoleEntity.getRoleName()), "roler_name", agentRoleEntity.getRoleName())
//                            .like(StringUtils.isNotEmpty(agentRoleEntity.getRoleMemo()), "roler_memo", agentRoleEntity.getRoleMemo())
            );
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
//            redisTemplate.opsForValue().set("test", list);
//            List<ParamEntity> list1 = (List<ParamEntity>)redisTemplate.opsForValue().get("test");
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "修改代理商角色信息", tags = {"updateAgentRoleById"}, notes = "修改代理商角色")
    @PutMapping("/updateAgentRoleById")
    public JSONResult<AgentRoleEntity> updateAgentRoleById(@ApiParam(value = "代理商角色实体类", name = "agentRoleEntity") AgentRoleEntity agentRoleEntity){
        JSONResult<AgentRoleEntity> jsonResult = new JSONResult<AgentRoleEntity>();
        try {
            if (this.agentRoleMapper.updateById(agentRoleEntity) > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分配代理商角色权限")
    public JSONResult<AgentRoleEntity> updateAgentRoleAuth(){
        JSONResult<AgentRoleEntity> jsonResult = new JSONResult<AgentRoleEntity>();
        return jsonResult;
    }
}
